package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsArea;
import com.csun.dsms.model.domain.DsmsRegulatorScore;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.enums.RoleCodeEnum;
import com.csun.dsms.model.vo.RegulatorScoreVo;
import com.csun.dsms.service.DsmsRegulatorScoreService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/regulator/score")
public class DsmsRegulatorScoreController {

    @Resource
    private DsmsRegulatorScoreService dsmsRegulatorScoreServiceImpl;

    /*
    *监督人员评价的添加
    * */
    @Authorization
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Wrapper regulatorScoreAdd(@CurrentUser DsmsUser login, @RequestBody DsmsRegulatorScore dsmsRegulatorScore){
        dsmsRegulatorScore.setRegulatorId(login.getId());
        dsmsRegulatorScore.setRegulatorName(login.getUserName());
        dsmsRegulatorScore.setCreator(login.getLoginName());
        dsmsRegulatorScore.setCreatorId(login.getId());
        dsmsRegulatorScore.setLastOperator(login.getLoginName());
        dsmsRegulatorScore.setLastOperatorId(login.getId());
        /*
        * 查询该监督员所属单位，网格，片区，服务公司
        * */
        DsmsRegulatorScore drs = dsmsRegulatorScoreServiceImpl.queryInformation(dsmsRegulatorScore);
        dsmsRegulatorScore.setUnitId(drs.getUnitId());
        dsmsRegulatorScore.setUnitName(drs.getUnitName());
        dsmsRegulatorScore.setCompanyId(drs.getCompanyId());
        dsmsRegulatorScore.setCompanyName(drs.getCompanyName());
        dsmsRegulatorScore.setAreaId(drs.getAreaId());
        dsmsRegulatorScore.setAreaName(drs.getAreaName());
        /*
        * 查询该月份是否已评价
        * */
        DsmsRegulatorScore regulatorScore = new DsmsRegulatorScore();
        regulatorScore.setRegulatorId(dsmsRegulatorScore.getRegulatorId());
        regulatorScore.setMonth(dsmsRegulatorScore.getMonth());
        List<RegulatorScoreVo> query = dsmsRegulatorScoreServiceImpl.query(regulatorScore);
        if(query.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该月份已完成评价");
        }
        Integer i = dsmsRegulatorScoreServiceImpl.save(dsmsRegulatorScore);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     *监督人员评价的删除
     * */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Wrapper regulatorScoreDelete(@RequestBody DsmsRegulatorScore dsmsRegulatorScore){
        Integer i = dsmsRegulatorScoreServiceImpl.delete(dsmsRegulatorScore);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     *监督人员评价的修改
     * */
    @Authorization
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Wrapper regulatorScoreUpdate(@CurrentUser DsmsUser login,@RequestBody DsmsRegulatorScore dsmsRegulatorScore){
        dsmsRegulatorScore.setLastOperator(login.getLoginName());
        dsmsRegulatorScore.setLastOperatorId(login.getId());
        dsmsRegulatorScore.setUpdateTime(new Date());
        /*
         * 查询该月份是否已评价
         * */
        DsmsRegulatorScore regulatorScore = new DsmsRegulatorScore();
        regulatorScore.setRegulatorId(dsmsRegulatorScore.getRegulatorId());
        regulatorScore.setMonth(dsmsRegulatorScore.getMonth());
        List<RegulatorScoreVo> query = dsmsRegulatorScoreServiceImpl.query(regulatorScore);
        if(query.size()>0 && query.get(0).getId() != dsmsRegulatorScore.getId() ){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该月份已完成评价");
        }
        Integer i = dsmsRegulatorScoreServiceImpl.update(dsmsRegulatorScore);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     *监督人员评价的查询
     * */
    @Authorization
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Wrapper regulatorScoreQuery(@CurrentUser DsmsUser login,@RequestBody DsmsRegulatorScore dsmsRegulatorScore){
        if(!login.getRoleCode().equals("admin")){
            dsmsRegulatorScore.setRegulatorId(login.getId());
        }
        List<RegulatorScoreVo> dsmsRegulatorScores = dsmsRegulatorScoreServiceImpl.query(dsmsRegulatorScore);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(dsmsRegulatorScores));
    }

}
